package com.htgd.gardenexpo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeConPlantSeatDTO;
import com.htgd.gardenexpo.dto.GeConTheaterPlanDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConTheaterMovieBless;
import com.htgd.gardenexpo.entity.GeConTheaterPlan;
import com.htgd.gardenexpo.entity.GeConTheaterPlanMovie;
import com.htgd.gardenexpo.page.PageBase;
import com.htgd.gardenexpo.page.PlanQuery;
import com.htgd.gardenexpo.service.GeConTheaterPlanMovieService;
import com.htgd.gardenexpo.service.GeConTheaterPlanService;
import com.htgd.gardenexpo.service.IGeConTheaterTimeService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 放映计划 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-09
 */
@RestController
@RequestMapping("/api/theaterPlan")
@Api(tags = "剧场计划")
@Slf4j
public class GeConTheaterPlanController {

    @Autowired
    private GeConTheaterPlanService geConTheaterPlanService;

    @Autowired
    private GeConTheaterPlanMovieService geConTheaterPlanMovieService;


    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add(@RequestBody GeConTheaterPlanDTO geConTheaterPlanDTO, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String phone = TokenUtil.getUsernameFromToken(accessToken);
        log.info("剧场计划-添加：{}，{}",geConTheaterPlanDTO,phone);
        return geConTheaterPlanService.add(geConTheaterPlanDTO,phone);
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult edit(@RequestBody GeConTheaterPlanDTO geConTheaterPlanDTO) {
        GeConTheaterPlan geConTheaterPlan = new GeConTheaterPlan();
        BeanUtils.copyProperties(geConTheaterPlanDTO,geConTheaterPlan);
        geConTheaterPlan.setNum(geConTheaterPlanDTO.getMovieIds().size());
        geConTheaterPlanService.updateById(geConTheaterPlan);

        List<String> movieIds = geConTheaterPlanDTO.getMovieIds();
        for (String movieId : movieIds) {
            GeConTheaterPlanMovie exist = geConTheaterPlanMovieService.getByMovieAndPlan(movieId,geConTheaterPlanDTO.getId());
            if(exist!=null){//有
                continue;
            }else {
                GeConTheaterPlanMovie geConTheaterPlanMovie = new GeConTheaterPlanMovie();
                geConTheaterPlanMovie.setPlanId(geConTheaterPlan.getId());
                geConTheaterPlanMovie.setMovieId(movieId);
                geConTheaterPlanMovie.setTickets(0);
                geConTheaterPlanMovieService.save(geConTheaterPlanMovie);
            }
        }
        List<GeConTheaterPlanMovie> planMovies = geConTheaterPlanMovieService.getByPlan(geConTheaterPlanDTO.getId());
        for (GeConTheaterPlanMovie planMovie : planMovies) {
            boolean flag = false;
            for (String movieId : movieIds) {
                if(planMovie.getMovieId().equals(movieId)){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                geConTheaterPlanMovieService.removeById(planMovie.getId());
            }
        }
        return CommonResult.success("修改成功");
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(String id) {
        GeConTheaterPlan geConTheaterPlan = new GeConTheaterPlan();
        geConTheaterPlan.setId(id);
        geConTheaterPlan.setIsDelete("1");
        geConTheaterPlanService.updateById(geConTheaterPlan);
//        geConTheaterPlanService.removeById(id);
//        QueryWrapper<GeConTheaterPlanMovie> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("plan_id",id);
////        geConTheaterPlanMovieService.remove(queryWrapper);

        return CommonResult.success("删除成功");
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConTheaterPlanVO>> list(PlanQuery planQuery) {
        PageInfo<GeConTheaterPlanVO> list = geConTheaterPlanService.pageList(planQuery);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeConTheaterPlanVO> details(String id) {
        GeConTheaterPlanVO geConTheaterPlanVO = new GeConTheaterPlanVO();
        GeConTheaterPlan byId = geConTheaterPlanService.getById(id);
        BeanUtils.copyProperties(byId,geConTheaterPlanVO);
        return CommonResult.success("查询成功",geConTheaterPlanVO);
    }

    @PostMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult review(@RequestBody GeConTheaterPlan geConTheaterPlan) {
        if(geConTheaterPlan.getStatus()==3){//发布
            geConTheaterPlan.setEffectiveStatus(1);
        }
        if(geConTheaterPlan.getStatus()==4){
            geConTheaterPlan.setEffectiveStatus(0);
        }
        geConTheaterPlanService.updateById(geConTheaterPlan);
        return CommonResult.success("成功");
    }

    @PostMapping(value = "/changeEffectiveStatus")
    @ApiOperation("更改生效状态")
    public CommonResult changeEffectiveStatus(@RequestBody GeConTheaterPlan geConTheaterPlan) {
        geConTheaterPlanService.updateById(geConTheaterPlan);
        return CommonResult.success("成功");
    }

    @GetMapping(value = "/getTheaterHomePage")
    @ApiOperation("获取剧场首页")
    public CommonResult<TheaterHomePageVO> getTheaterHomePage(HttpServletRequest request) {
        TheaterHomePageVO theaterHomePageVO = geConTheaterPlanService.getTheaterHomePage(request);
        return CommonResult.success("成功",theaterHomePageVO);
    }

    @GetMapping(value = "/getVotingInformation")
    @ApiOperation("获取投票信息")
    public CommonResult<List<TheaterTicketListVO>> getVotingInformation(PageBase pageBase, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        PageInfo<TheaterTicketListVO> list = geConTheaterPlanService.getVotingInformation(pageBase,openId);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @PostMapping(value = "/changeSeat")
    @ApiOperation("确定选座")
    public CommonResult updateSeats(@RequestBody GeConPlantSeatDTO geConPlantSeat, HttpServletRequest request) {
        try {
            geConTheaterPlanService.updateSeats(geConPlantSeat,request);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error("您已预约完，不可再操作");
        }
        return CommonResult.success("预定成功");
    }

    @GetMapping(value = "/getAttendanceAndMovieTicketNums")
    @ApiOperation("获取上座率及目前各电影投票数量")
    public CommonResult<TheaterNumsVO> getAttendanceAndMovieTicketNums(@RequestParam(name = "date",required = false) String date,
                                                                       @RequestParam(name = "pageNum",required = true,defaultValue = "1")Integer pageNum,
                                                                       @RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize) {
        log.info("综管-获取投票获取上座率及目前各电影投票数量信息：{},{},{}",date,pageNum,pageSize);
        return geConTheaterPlanService.getAttendanceAndMovieTicketNums(date,pageNum,pageSize);
    }

    @GetMapping(value = "/getTheaterHomePageNew")
    @ApiOperation("获取剧场首页新")
    public CommonResult<TheaterHomePageNewVO> getTheaterHomePageNew(HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("剧场首页新：{}",openId);
        return geConTheaterPlanService.getTheaterHomePageNew(openId);
    }

    @GetMapping(value = "/getPlanDetails")
    @ApiOperation("计划详情")
    public CommonResult getPlanDetails(@RequestParam("id") String id,
                                       @RequestParam("movieId") String movieId) {
        log.info("计划详情：{},{}",id,movieId);
        return geConTheaterPlanService.getPlanDetails(id,movieId);
    }



}
